
#include "ThreadPool.h"
#include "ThreadPoolWorkerThread.h"

#include "OldDreamTools\Win32Tools\Utils.h"
#include "OldDreamTools\Win32Tools\Exception.h"



///////////////////////////////////////////////////////////////////////////////
// Using directives
///////////////////////////////////////////////////////////////////////////////

using OldDreamTools::Win32::CIOCompletionPort;
using OldDreamTools::Win32::CEvent;
using OldDreamTools::Win32::Output;
using OldDreamTools::Win32::CSocketServer;
using OldDreamTools::Win32::CIOBuffer;
using OldDreamTools::Win32::CException;

///////////////////////////////////////////////////////////////////////////////
// CThreadPool
///////////////////////////////////////////////////////////////////////////////

CThreadPool::CThreadPool(
   size_t initialThreads,
   size_t minThreads,
   size_t maxThreads,
   size_t maxDormantThreads,
   size_t poolMaintPeriod,
   size_t dispatchTimeout,
   size_t maxThreadsDispatchTimeout)
   :  OldDreamTools::Win32::CThreadPool(
         initialThreads,
         minThreads,
         maxThreads,
         maxDormantThreads,
         poolMaintPeriod,
         dispatchTimeout,
         maxThreadsDispatchTimeout)
{

}

OldDreamTools::Win32::CThreadPool::WorkerThread *CThreadPool::CreateWorkerThread(
   CIOCompletionPort &iocp,
   CEvent &messageReceivedEvent,
   OldDreamTools::Win32::CThreadPool &pool)
{

	//CServerSet::gOnly().LoadPlugin();
   return new CThreadPoolWorkerThread(
      iocp,
      messageReceivedEvent,
      pool);
}

void CThreadPool::OnThreadStarted()
{
   Output(_T("OnThreadStarted"));
}

void CThreadPool::OnThreadInitialised()
{
   Output(_T("OnThreadInitialised"));
}

void CThreadPool::OnProcessingStarted()
{
   Output(_T("OnProcessingStarted"));
}

void CThreadPool::OnReceivedMessage()
{
   Output(_T("OnReceivedMessage"));
}

void CThreadPool::OnProcessingStopped()
{
   Output(_T("OnProcessingStopped"));
}

void CThreadPool::OnThreadStopped()
{
   Output(_T("OnThreadStopped"));
}

void CThreadPool::DispatchConnectionEstablished(
   OldDreamTools::Win32::CSocketServer::Socket *pSocket,
   OldDreamTools::Win32::CIOBuffer *pAddress)
{
   DoDispatch(pSocket, pAddress, ConnectionEstablished);
}

void CThreadPool::DispatchConnectionClosing(
   OldDreamTools::Win32::CSocketServer::Socket *pSocket)
{
   DoDispatch(pSocket, 0, ConnectionClosing);
}

void CThreadPool::DispatchConnectionClosed(
   OldDreamTools::Win32::CSocketServer::Socket *pSocket)
{
   DoDispatch(pSocket, 0, ConnectionClosed);
}

void CThreadPool::DispatchReadCompleted(
   OldDreamTools::Win32::CSocketServer::Socket *pSocket,
   OldDreamTools::Win32::CIOBuffer *pBuffer)
{
   DoDispatch(pSocket, pBuffer, ReadCompleted);
}

void CThreadPool::DoDispatch(
   CSocketServer::Socket *pSocket,
   CIOBuffer *pBuffer,
   DispatchEvents event)
{
   pSocket->AddRef();

   if (pBuffer)
   {
      pBuffer->AddRef();
   }
   else if (event != ConnectionClosed && event != ConnectionClosing)
   {
      throw CException(_T("CThreadPool::DoDispatch()"), _T("Unexpected: Only the ConnectionClosed and ConnectionClosing events can have a null pBuffer"));
   }

   Dispatch(
      reinterpret_cast<ULONG_PTR>(pSocket),
      event,
      pBuffer);
}

///////////////////////////////////////////////////////////////////////////////
// Lint options
//
//lint -restore
//
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// End of file
///////////////////////////////////////////////////////////////////////////////
